import java.util.Arrays;

public class Solution2 {
    public static void main(String[] args) {
        int[] nums = {5,7,7,8,8,10};
        int target = 8;
        int[] ret = searchRange(nums,target);
        System.out.println(Arrays.toString(ret));
    }
    public static int[] searchRange(int[] nums, int target) {
        int[] ret = new int[]{-1,-1};
        //处理边界情况
        if(nums.length == 0) {
            return ret;
        }

        int left = 0;
        int right = nums.length - 1;

        //1.二分左端点
        while(left < right) {
            int mid = left + (right - left) / 2;
            if(nums[mid] < target) {
                left = mid + 1;
            }else {
                right = mid;
            }
        }
        //判断是否有结果
        if(nums[left] != target) {
            return ret;
        }else {
            ret[0] = left;
        }
        //2.二分右端点
        right = nums.length - 1;
        while(left < right) {
            int mid = left + (right - left + 1) / 2;
            if(nums[mid] > target) {
                right = mid - 1;
            }else {
                left = mid;
            }
        }
        ret[1] = right;
        return ret;
    }
}